package com.dz.basic.web;

import io.swagger.annotations.ApiOperation;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

import lombok.extern.slf4j.Slf4j;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import springfox.documentation.annotations.ApiIgnore;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.dz.basic.LoanQualificationVO;
import com.dz.basic.entity.RelationQualification;
import com.dz.basic.service.RelationQualificationService;

/**
 * <p>
 * 关联贷款资质数据，包括客户资质，商品贷款资质，订单资质 前端控制器
 * </p>
 *
 * @author star
 * @since 2017-06-27
 */
@RestController
@Slf4j
public class RelationQualificationControler {

	@Autowired
	private RelationQualificationService service;

	@RequestMapping("/relationQualification/addBatch")
	@ApiIgnore
	public boolean addBatch(@RequestBody List<RelationQualification> relationQualifications) {
		return service.insertBatch(relationQualifications);
	}

	@RequestMapping(value = "/relation/list", method = RequestMethod.GET)
	@ApiOperation(value = "获取资质数据")
	public List<LoanQualificationVO> getById(@RequestParam("itemId") Long id, @RequestParam("classid") Long classId) {
		Wrapper<RelationQualification> reWrapper = new EntityWrapper<RelationQualification>();
		reWrapper.eq("fitemid", id).eq("fitemclassid", classId).orderBy("findex").orderBy("fparentid");
		List<RelationQualification> relationQualifications = service.selectList(reWrapper);
		List<LoanQualificationVO> voList = new ArrayList<LoanQualificationVO>();
		for (RelationQualification loanQualification : relationQualifications) {
			LoanQualificationVO vo = new LoanQualificationVO();
			try {
				if (loanQualification.getParentid() == -1) {
					BeanUtils.copyProperties(vo, loanQualification);
					changeData(vo, relationQualifications);
					voList.add(vo);
				}
			} catch (IllegalAccessException | InvocationTargetException e) {
				log.error(e.getMessage(), e);
			}
		}
		return voList;
	}

	private void changeData(LoanQualificationVO vo, List<RelationQualification> loanQualifications) {
		String type = vo.getType();
		if ("list".equals(type) && vo.getRootid() != vo.getParentid()) {
			List<RelationQualification> childList = new ArrayList<RelationQualification>();
			for (RelationQualification loanQualification : loanQualifications) {
				if (loanQualification.getParentid() == vo.getId()) {
					childList.add(loanQualification);
					String itemType = loanQualification.getType();
					if ("list".equals(itemType) && vo.getRootid() != vo.getParentid()) {
						LoanQualificationVO itemVO = new LoanQualificationVO();
						try {
							BeanUtils.copyProperties(itemVO, loanQualification);
							changeData(itemVO, loanQualifications);
						} catch (IllegalAccessException | InvocationTargetException e) {
							log.error(e.getMessage(), e);
						}
					}
				}
			}
			vo.setChildList(childList);
		}
	}
}
